﻿using AutoMapper;
using HealthcareSystem.API.Write.Application.Command.Disease;
using HealthcareSystem.API.Write.Application.Command.ElementCommand;
using HealthcareSystem.Domain.BasicSettings.Diseasemanagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.Disease
{
    /// <summary>
    /// 病例编辑
    /// </summary>
    public class PutDiseaseCommandHandler : IRequestHandler<PutDiseaseCommand, ApiResult<int>>
    {
        IBaseRepository<Diseasemanage> diseasemanageHandler;
        IMapper mapper;
        public PutDiseaseCommandHandler(IBaseRepository<Diseasemanage> diseasemanageHandler, IMapper mapper)
        {
            this.diseasemanageHandler = diseasemanageHandler;
            this.mapper = mapper;
        }
        /// <summary>
        /// 病例编辑处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(PutDiseaseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();

            var list = diseasemanageHandler.GetAll().FirstOrDefault(x => x.CodeId == request.CodeId);
            var quer = mapper.Map(request, list);
            await diseasemanageHandler.UpdAsync(quer);
            return await Task.FromResult(result);
        }
    }
}
